<!--

    Copyright © 2016-2025 The Thingsboard Authors

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<div [formGroup]="fieldFormGroup" class="calculated-field-dialog-container flex size-full max-w-4xl flex-col">
  <mat-toolbar color="primary">
    <h2>{{ 'entity.type-calculated-field' | translate}}</h2>
    <span class="flex-1"></span>
    <div tb-help="calculatedField"></div>
    <button mat-icon-button
            (click)="cancel()"
            type="button">
      <mat-icon class="material-icons">close</mat-icon>
    </button>
  </mat-toolbar>
  <div mat-dialog-content class="flex-1">
    <div class="tb-form-panel no-border no-padding">
      <div class="tb-form-panel">
        <div class="tb-form-panel-title">{{ 'common.general' | translate }}</div>
        <div class="flex items-center gap-2">
          <mat-form-field class="flex-1" appearance="outline" subscriptSizing="dynamic">
            <mat-label>{{ 'entity-field.title' | translate }}</mat-label>
            <input matInput maxlength="255" formControlName="name" required>
            @if (fieldFormGroup.get('name').errors && fieldFormGroup.get('name').touched) {
              <mat-error>
                @if (fieldFormGroup.get('name').hasError('required')) {
                  {{ 'common.hint.title-required' | translate }}
                } @else if (fieldFormGroup.get('name').hasError('pattern')) {
                  {{ 'common.hint.title-pattern' | translate }}
                } @else if (fieldFormGroup.get('name').hasError('maxlength')) {
                  {{ 'common.hint.title-max-length' | translate }}
                }
              </mat-error>
            }
          </mat-form-field>
          <tb-entity-debug-settings-button
            formControlName="debugSettings"
            [class.mb-5]="fieldFormGroup.get('name').errors && fieldFormGroup.get('name').touched"
            [entityType]="EntityType.CALCULATED_FIELD"
            [additionalActionConfig]="additionalDebugActionConfig"
          />
        </div>
        <mat-form-field appearance="outline" subscriptSizing="dynamic">
          <mat-label>{{ 'common.type' | translate }}</mat-label>
          <mat-select formControlName="type">
            @for (type of fieldTypes; track type) {
              <mat-option [value]="type">{{ CalculatedFieldTypeTranslations.get(type) | translate}}</mat-option>
            }
          </mat-select>
        </mat-form-field>
      </div>
      <ng-container [formGroup]="configFormGroup">
        @if (fieldFormGroup.get('type').value !== CalculatedFieldType.GEOFENCING) {
          <div class="tb-form-panel">
            <div class="tb-form-panel-title tb-required">{{ 'calculated-fields.arguments' | translate }}</div>
            <tb-calculated-field-arguments-table formControlName="arguments"
                                                 [entityId]="data.entityId"
                                                 [tenantId]="data.tenantId"
                                                 [entityName]="data.entityName"
                                                 [calculatedFieldType]="fieldFormGroup.get('type').value"/>
          </div>
          <div class="tb-form-panel no-gap">
            <div class="tb-form-panel-title tb-required">
              {{ (fieldFormGroup.get('type').value === CalculatedFieldType.SIMPLE ? 'calculated-fields.expression' : 'calculated-fields.type.script' ) | translate }}
            </div>
            <mat-form-field class="mt-3" appearance="outline" subscriptSizing="dynamic" [class.hidden]="fieldFormGroup.get('type').value !== CalculatedFieldType.SIMPLE">
              <input matInput formControlName="expressionSIMPLE" maxlength="255" [placeholder]="'(temperature - 32) / 1.8'" required>
              <div  matSuffix
                    class="pr-2"
                    [tb-help-popup]="'math/math-methods_fn'"
                    tb-help-popup-placement="left"
                    [tb-help-popup-style]="{maxWidth: '970px'}">
              </div>
              @if (configFormGroup.get('expressionSIMPLE').errors && configFormGroup.get('expressionSIMPLE').touched) {
                <mat-error>
                  @if (configFormGroup.get('expressionSIMPLE').hasError('required')) {
                    {{ 'calculated-fields.hint.expression-required' | translate }}
                  } @else if (configFormGroup.get('expressionSIMPLE').hasError('pattern')) {
                    {{ 'calculated-fields.hint.expression-invalid' | translate }}
                  } @else if (configFormGroup.get('expressionSIMPLE').hasError('maxLength')) {
                    {{ 'calculated-fields.hint.expression-max-length' | translate }}
                  }
                </mat-error>
              } @else {
                <mat-hint>{{ 'calculated-fields.hint.expression' | translate }}</mat-hint>
              }
            </mat-form-field>
            <div [class.hidden]="fieldFormGroup.get('type').value !== CalculatedFieldType.SCRIPT">
              <tb-js-func required
                          formControlName="expressionSCRIPT"
                          functionName="calculate"
                          [functionArgs]="functionArgs$ | async"
                          [disableUndefinedCheck]="true"
                          [scriptLanguage]="ScriptLanguage.TBEL"
                          [highlightRules]="argumentsHighlightRules$ | async"
                          [editorCompleter]="argumentsEditorCompleter$ | async"
                          [helpPopupStyle]="{ width: '1200px' }"
                          helpId="calculated-field/expression_fn">
                <div toolbarPrefixButton class="tb-primary-background tbel-script-lang-chip">{{ 'api-usage.tbel' | translate }}</div>
                <button toolbarSuffixButton
                        mat-icon-button
                        matTooltip="{{ 'calculated-fields.test-script-function' | translate }}"
                        matTooltipPosition="above"
                        class="tb-mat-32"
                        [disabled]="configFormGroup.get('arguments').invalid"
                        (click)="onTestScript()">
                  <mat-icon class="material-icons" color="primary">bug_report</mat-icon>
                </button>
              </tb-js-func>
              <div>
                <button mat-button mat-raised-button color="primary"
                        type="button"
                        (click)="onTestScript()"
                        [disabled]="configFormGroup.get('arguments').invalid">
                  {{ 'calculated-fields.test-script-function' | translate }}
                </button>
              </div>
            </div>
          </div>
        } @else {
          <div class="tb-form-panel">
            <div class="tb-form-panel-title tb-required" tb-hint-tooltip-icon="{{ 'calculated-fields.hint.entity-coordinates' | translate }}">
              {{ 'calculated-fields.entity-coordinates' | translate }}
            </div>
            <div class="flex items-start gap-3" [formGroup]="coordinatesFormGroup">
              <tb-entity-key-autocomplete class="flex-1"
                                          placeholder="{{ 'calculated-fields.latitude-time-series-key' | translate }}"
                                          requiredText="{{ 'calculated-fields.latitude-time-series-key-required' | translate }}"
                                          formControlName="latitudeKeyName"
                                          [dataKeyType]="DataKeyType.timeseries"
                                          [entityFilter]="currentEntityFilter"/>
              <tb-entity-key-autocomplete class="flex-1"
                                          placeholder="{{ 'calculated-fields.longitude-time-series-key' | translate }}"
                                          requiredText="{{ 'calculated-fields.longitude-time-series-key-required' | translate }}"
                                          formControlName="longitudeKeyName"
                                          [dataKeyType]="DataKeyType.timeseries"
                                          [entityFilter]="currentEntityFilter"/>
            </div>
          </div>

          <div class="tb-form-panel">
            <div class="tb-form-panel-title tb-required" tb-hint-tooltip-icon="{{ 'calculated-fields.hint.geofencing-zone-groups' | translate }}">
              {{ 'calculated-fields.geofencing-zone-groups' | translate }}
            </div>
            <tb-calculated-field-geofencing-zone-groups-table formControlName="zoneGroups"
                                                              [entityId]="data.entityId"
                                                              [tenantId]="data.tenantId"
                                                              [entityName]="data.entityName"/>
            <div class="tb-form-row space-between flex-1 columns-xs" [class.!hidden]="!isRelatedEntity">
              <mat-slide-toggle class="mat-slide" formControlName="scheduledUpdateEnabled">
                <div tb-hint-tooltip-icon="{{'calculated-fields.hint.zone-group-refresh-interval' | translate}}">
                  {{ 'calculated-fields.zone-group-refresh-interval' | translate }}
                </div>
              </mat-slide-toggle>
              <div class="flex flex-row items-center justify-start gap-2">
                <tb-time-unit-input required
                                    inlineField
                                    requiredText="{{ 'calculated-fields.hint.zone-group-refresh-interval-required' | translate }}"
                                    minErrorText="{{ 'calculated-fields.hint.zone-group-refresh-interval-min' | translate: {min: minAllowedScheduledUpdateIntervalInSecForCF}  }}"
                                    [minTime]="minAllowedScheduledUpdateIntervalInSecForCF"
                                    formControlName="scheduledUpdateInterval">
                </tb-time-unit-input>
              </div>
            </div>
          </div>
        }
        <div class="tb-form-panel" [formGroup]="outputFormGroup">
          <div class="tb-form-panel-title">{{ 'calculated-fields.output' | translate }}</div>
          <div class="flex items-center gap-3">
            <mat-form-field class="flex-1" appearance="outline" subscriptSizing="dynamic">
              <mat-label>{{ 'calculated-fields.output-type' | translate }}</mat-label>
              <mat-select formControlName="type">
                @for (type of outputTypes; track type) {
                  <mat-option [value]="type">{{ OutputTypeTranslations.get(type) | translate}}</mat-option>
                }
              </mat-select>
            </mat-form-field>
            @if (outputFormGroup.get('type').value === OutputType.Attribute
            && (data.entityId.entityType === EntityType.DEVICE || data.entityId.entityType === EntityType.DEVICE_PROFILE)) {
              <mat-form-field class="flex-1" appearance="outline" subscriptSizing="dynamic">
                <mat-label>{{ 'calculated-fields.attribute-scope' | translate }}</mat-label>
                <mat-select formControlName="scope" class="w-full">
                  <mat-option [value]="AttributeScope.SERVER_SCOPE">
                    {{ 'calculated-fields.server-attributes' | translate }}
                  </mat-option>
                  <mat-option [value]="AttributeScope.SHARED_SCOPE">
                    {{ 'calculated-fields.shared-attributes' | translate }}
                  </mat-option>
                </mat-select>
              </mat-form-field>
            }
          </div>
          @if (fieldFormGroup.get('type').value === CalculatedFieldType.SIMPLE) {
            <div class="flex items-start gap-3">
              <mat-form-field class="flex-1" appearance="outline" subscriptSizing="dynamic">
                <mat-label>
                  {{ (outputFormGroup.get('type').value === OutputType.Timeseries
                    ? 'calculated-fields.timeseries-key'
                    : 'calculated-fields.attribute-key')
                  | translate }}
                </mat-label>
                <input matInput formControlName="name" required>
                @if (outputFormGroup.get('name').errors && outputFormGroup.get('name').touched) {
                  <mat-error>
                    @if (outputFormGroup.get('name').hasError('required')) {
                      {{ 'common.hint.key-required' | translate }}
                    } @else if (outputFormGroup.get('name').hasError('pattern')) {
                      {{ 'common.hint.key-pattern' | translate }}
                    } @else if (outputFormGroup.get('name').hasError('maxlength')) {
                      {{ 'common.hint.key-max-length' | translate }}
                    }
                  </mat-error>
                }
              </mat-form-field>
              <mat-form-field class="flex-1" appearance="outline" subscriptSizing="dynamic">
                <mat-label>{{ 'calculated-fields.decimals-by-default' | translate }}</mat-label>
                <input matInput type="number" formControlName="decimalsByDefault">
                @if (outputFormGroup.get('decimalsByDefault').errors && outputFormGroup.get('decimalsByDefault').touched) {
                  <mat-error>{{ 'calculated-fields.hint.decimals-range' | translate }}</mat-error>
                }
              </mat-form-field>
            </div>
            <div class="tb-form-row" [formGroup]="configFormGroup" *ngIf="outputFormGroup.get('type').value === OutputType.Timeseries">
              <mat-slide-toggle class="mat-slide" formControlName="useLatestTs">
                <div tb-hint-tooltip-icon="{{ 'calculated-fields.hint.use-latest-timestamp' | translate }}" translate>
                  calculated-fields.use-latest-timestamp
                </div>
              </mat-slide-toggle>
            </div>
          }
        </div>
      </ng-container>
    </div>
  </div>
  <div mat-dialog-actions class="justify-end">
    <button mat-button color="primary"
            type="button"
            cdkFocusInitial
            (click)="cancel()">
      {{ 'action.cancel' | translate }}
    </button>
    <button mat-raised-button color="primary"
            (click)="add()"
            [disabled]="(isLoading$ | async) || fieldFormGroup.invalid || !fieldFormGroup.dirty">
      {{ data.buttonTitle | translate }}
    </button>
  </div>
</div>
